package com.hy.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hy.utils.glm.ChatGLMModel;
import com.hy.utils.glm.Prompt;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ChatGLM相关工具类
 */
public class ChatGLMUtils {

    /**
     * ChatGLM API URL
     */
    public static final String API_URL = "https://open.bigmodel.cn/api/paas/v3/model-api/" + ChatGLMModel.CHATGLM_4_Flash.key + "/sse-invoke";

    /**
     * 生成ChatGLM请求体
     *
     * @param content 请求内容
     * @return 请求体JSON字符串
     */
    public static String generateBodyString(String content) {
        String requestId = String.format("hy-%d", System.currentTimeMillis());
        float temperature = 0.9f;
        float topP = 0.7f;
        String sseFormat = "data";      // 数据格式
        boolean incremental = true;     // 增量式推送

        List<Prompt> promptList = new ArrayList<>();
        Prompt prompt = Prompt.builder()
                .role("user")
                .content(content)
                .build();
        promptList.add(prompt);

        Map<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("request_id", requestId);
        paramsMap.put("prompt", promptList);
        paramsMap.put("incremental", incremental);
        paramsMap.put("temperature", temperature);
        paramsMap.put("top_p", topP);
        paramsMap.put("sseFormat", sseFormat);

        try {
            return new ObjectMapper().writeValueAsString(paramsMap);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("生成ChatGLM请求体失败", e);
        }
    }
}